package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.system.domain.TbProject;
import com.ruoyi.system.mapper.TbProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbRecordMapper;
import com.ruoyi.system.domain.TbRecord;
import com.ruoyi.system.service.ITbRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 捐赠记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-23
 */
@Service
public class TbRecordServiceImpl implements ITbRecordService 
{
    @Autowired
    private TbRecordMapper tbRecordMapper;

    @Autowired
    private TbProjectMapper tbProjectMapper;

    /**
     * 查询捐赠记录
     * 
     * @param id 捐赠记录主键
     * @return 捐赠记录
     */
    @Override
    public TbRecord selectTbRecordById(Long id)
    {
        return tbRecordMapper.selectTbRecordById(id);
    }

    /**
     * 查询捐赠记录列表
     * 
     * @param tbRecord 捐赠记录
     * @return 捐赠记录
     */
    @Override
    public List<TbRecord> selectTbRecordList(TbRecord tbRecord)
    {
        return tbRecordMapper.selectTbRecordList(tbRecord);
    }

    /**
     * 新增捐赠记录
     * 
     * @param tbRecord 捐赠记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbRecord(TbRecord tbRecord)
    {
        // 更新捐赠项目的已募集、支持次数
        TbProject tbProject = tbProjectMapper.selectTbProjectById(tbRecord.getProjectId());
        BigDecimal sum = tbProject.getCurrentAmount().add(tbRecord.getDonationMoney());

        tbProject.setCurrentAmount(sum);
        tbProject.setFrequency(tbProject.getFrequency() + 1);
        tbProjectMapper.updateTbProject(tbProject);

        return tbRecordMapper.insertTbRecord(tbRecord);
    }

    /**
     * 修改捐赠记录
     * 
     * @param tbRecord 捐赠记录
     * @return 结果
     */
    @Override
    public int updateTbRecord(TbRecord tbRecord)
    {
        return tbRecordMapper.updateTbRecord(tbRecord);
    }

    /**
     * 批量删除捐赠记录
     * 
     * @param ids 需要删除的捐赠记录主键
     * @return 结果
     */
    @Override
    public int deleteTbRecordByIds(Long[] ids)
    {
        return tbRecordMapper.deleteTbRecordByIds(ids);
    }

    /**
     * 删除捐赠记录信息
     * 
     * @param id 捐赠记录主键
     * @return 结果
     */
    @Override
    public int deleteTbRecordById(Long id)
    {
        return tbRecordMapper.deleteTbRecordById(id);
    }
}
